/*
 *   MetrixViewer - A simple app for viewer Metrix data. Uses XML data from Webosroundup's Metrix data and graphs it using Google charts API.
 *   http://code.google.com/p/metrixviewer/
 *   Copyright (C) 2010  Wes Brown
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
function MainAssistant() {

	this.resultList = [];

}

MainAssistant.prototype.setup = function() {

	
	this.controller.document.body.className = 'palm-dark';
	
	
	//Metrix command
	MetrixViewer.Metrix.postDeviceData();


	this.appsCommandMenuModel = {
		visible: true,
		items: [
			{},
			{ label: $L("Add"), command: 'go-addApp', width: 90 } ,
			{ }
		]
	};
	
	//Bottom of page menu widget
	this.controller.setupWidget( Mojo.Menu.commandMenu, {menuClass: 'no-fade'}, this.appsCommandMenuModel );
	
	//List of apps 
	this.appsListAttribs = {
		itemTemplate: "main/appsListItem",
		swipeToDelete: true
	};
	
    this.appsListModel = {            
        items: this.resultList
    };
	this.controller.setupWidget( "appsList" , this.appsListAttribs, this.appsListModel);
	
	
	//Tap a host from list
	this.controller.listen(this.controller.get( "appsList" ), Mojo.Event.listTap, this.chooseList.bind(this));
		
	//Delete from list
	this.controller.listen(this.controller.get( "appsList" ), Mojo.Event.listDelete, this.deleteList.bind(this));


};

MainAssistant.prototype.activate = function(event) {

	//Populate list using cookie
	this.resultList.clear();
	
	if(MetrixViewer.appsCookieObject) {
		Object.extend(this.resultList,MetrixViewer.appsCookieObject);
		this.controller.modelChanged(this.appsListModel);
	} else {
		MetrixViewer.appsCookieObject = [];
		
		this.resultList.push({"appname": "Metrix Viewer", "packageId": "com.thewbman.metrixviewer"});
		this.controller.modelChanged(this.appsListModel);
	}
	
	
	
	if(MetrixViewer.customCookieObject) {
		//We have the custom metrics cookie
	} else {
		MetrixViewer.customCookieObject = [];
		MetrixViewer.customCookie.put(MetrixViewer.customCookieObject);
	}
	
};

MainAssistant.prototype.deactivate = function(event) {

	MetrixViewer.appsCookie.put(MetrixViewer.appsCookieObject);

};

MainAssistant.prototype.cleanup = function(event) {

};

MainAssistant.prototype.handleCommand = function(event) {

	if(event.type == Mojo.Event.command) {
		Mojo.Log.info("command: "+event.command);

		switch(event.command) {
			case 'go-addApp':
				Mojo.Controller.stageController.pushScene("addApp");
			  break;

		}
		
	}
	
}



MainAssistant.prototype.chooseList = function(event) {

	Mojo.Log.info("Selected app: %j",event.item);
	
	MetrixViewer.appname = event.item.appname;
	MetrixViewer.packageId = event.item.packageId;
	
	//Mojo.Controller.stageController.pushScene("dataSelector");
	Mojo.Controller.stageController.pushScene("allData");
	
};

MainAssistant.prototype.deleteList = function(event) {
	
	var appname_in = event.item.appname;
	var packageId_in = event.item.packageId;
	
	
	Mojo.Log.info("Deleting app: %j",event.item);
	
	var newList = [];
	var s = {};
	
	for(var i = 0; i < MetrixViewer.appsCookieObject.length; i++) {
	
		s = MetrixViewer.appsCookieObject[i];
		
		if((s.appname == appname_in)&&(s.packageId == packageId_in)) {
			//App to delete
		} else {
			newList.push(s);
		}
	
	}
	
	//Update cookie
	MetrixViewer.appsCookieObject.clear();
	Object.extend(MetrixViewer.appsCookieObject,newList);
	
	MetrixViewer.appsCookie.put(MetrixViewer.appsCookieObject);
	
};
